﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using LogLib;
using ComLib.ConfigService;
using System.Reflection;
using ComLib;

namespace PhotoProcess
{
    internal class PreprocessDirectory
    {
        private static string AssemblyDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase).Substring(6);

        public static string IniFileName = Path.Combine(AssemblyDirectory, "config", "Directory.ini");

        private string _rootDir = "D:\\Printing";
        public string RootDir
        {
            get { return _rootDir; }
            set { _rootDir = value; }
        }

        private DateTime _todayDate = DateTime.Now.Date;

        private Dictionary<string, DirectoryInfo> dicDirectory = new Dictionary<string, DirectoryInfo>();

        public PreprocessDirectory()
        {
            if (!File.Exists(IniFileName))
            {
                Save();
            }
            Load();
        }

        /// <summary>
        /// 根据关键字创建当天输出目录
        /// </summary>
        /// <param name="key"></param>
        private void CreatTodaySaveDirectory(string key)
        {
            if (!Directory.Exists(RootDir))
            {
                Directory.CreateDirectory(RootDir);
                Log.WriteAsync("创建新目录:" + RootDir);
            }
            string directory = string.Format(@"{0}\{1}\{2}\{3}",
                   RootDir,
                   key,
                   System.DateTime.Now.ToString("yyyyMM"),
                   System.DateTime.Now.ToString("dd"));
            DirectoryInfo dirInfo = new DirectoryInfo(directory);
            if (!dicDirectory.ContainsKey(key))
            {
                dicDirectory.Add(key, dirInfo);
            }
            else
            {
                dicDirectory[key] = dirInfo;
            }
            if (!dirInfo.Exists)
            {
                dirInfo.Create();             
                Log.WriteAsync("创建新目录:" + directory);
            }
        }

        /// <summary>
        /// 检查并更新当日目录
        /// </summary>
        /// <returns></returns>
        private void CheckAndUpdate(string key)
        {
            if (!_todayDate.Equals(System.DateTime.Now.Date))
            {
                _todayDate = System.DateTime.Now.Date;
            }
            if (!dicDirectory.ContainsKey(key))
            {
                CreatTodaySaveDirectory(key);
            }
            else
            {
                DirectoryInfo dirInfo = dicDirectory[key];
                if (!dirInfo.CreationTime.Date.Equals(_todayDate))
                {
                    CreatTodaySaveDirectory(key);
                }
            }
        }
 
        /// <summary>
        /// 保存成果照片的完整目录地址
        /// </summary>
        public string Printing
        {
            get
            {
                CheckAndUpdate("PxlCrop");
                return dicDirectory["PxlCrop"].FullName;
            }
        }
   
        public bool Save()
        {
            IniConfigFile.WriteValue("config", "RootDir", RootDir, IniFileName);
            return true;
        }

        public bool Load()
        {
            try
            {
                RootDir = IniConfigFile.IniValue("config", "RootDir", IniFileName);
            }
            catch (Exception ex)
            {
                string message = string.Format("配置文件加载错误:\n\t{0}", ex.Message);
                Log.WriteException(ex.ToString());
                return false;
            }
            return true;
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder("打印照片输出目录:");
            builder.Append("[打印输出目录]: " + RootDir);
            return builder.ToString();
        }
    }
}
